﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Belmost4PDA.Fasade;

namespace Belmost4PDA.Logic
{
    /// <summary>
    /// чек бокс c едитбоксом которые енаблиться при выборе чекбокса (не выключаеться если в его группе ктото включился)
    /// </summary>
    public class defStrategyCheckEdit:defStrategyCheck
    {
        protected TextBox textbox = null;
        protected Label label = null;
        public defStrategyCheckEdit(DefectsReader.brDefItem brDefItem, DefectsReader.DefItem defItem, brDef brDefOwner)
            : base(brDefItem, defItem, brDefOwner)
        {
            textbox = new TextBox();
            //textbox.MaxLength = 11;//defscope precision=10,scale=6, so maxlength=precision+','=11
            textbox.Text = brDefItem.defScope.Replace(',', '.');
            if (textbox.Text.LastIndexOf('.') != -1)
                textbox.Text = textbox.Text.TrimEnd('0');
            textbox.Text = textbox.Text.TrimEnd('.');
            if (textbox.Text == "") textbox.Text = "0";
            _Value = brDefItem.defScope.Replace(',', '.');
            label = new Label();
            label.Text = defItem.clsShort;
            if(!_isChecked)
                DisableEdit();
            else 
                EnableEdit();
        }

        public override void AddToForm(Form _form)
        {
            base.AddToForm(_form);
            // add text box
            int RelY = (int)_form.Tag;
            RelY += 5;
            textbox.Left = 10;
            textbox.Width = _form.Width/2 - 12;
            textbox.Top = RelY;
            // add label
            label.Left = _form.Width/2 + 3;
            label.Width = _form.Width/2 - 8;
            label.Top = RelY;
            RelY += textbox.Height;
            _form.Tag = RelY;
            _form.Controls.Add(textbox);
            _form.Controls.Add(label);
        }

        public override void FromControls()
        {
            base.FromControls();
            Value = textbox.Text;
        }

        public override void afterCheckOff()
        {
            DisableEdit();
        }

        private void DisableEdit()
        {
            textbox.Text = "0";
            textbox.Enabled = false;
        }

        public override void afterCheckOn()
        {
            EnableEdit();
        }

        private void EnableEdit()
        {
            textbox.Enabled = true;
        }
    }
}
